﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizzCore;
using BizzUI.Forms;

namespace BizzUI.UserControls
{
    public partial class MessageHistory : UserControl
    {

        public MessageHistory()
        {
            InitializeComponent();
            List<MessageEntity> msgList = BizzSystem.getInstance().getMessages(100);
            initMessageHistory(msgList);
        }

        public MessageHistory(List<MessageEntity> msgList)
        {
            InitializeComponent();
            initMessageHistory(msgList);
        }

        private void initMessageHistory(List<MessageEntity> msgList)
        {
            
            dgMessages.DataSource = msgList;
            

            DataGridViewImageColumn attributesColumn = new DataGridViewImageColumn();
            attributesColumn.HeaderText = "Atributos";
            attributesColumn.Image = global::BizzUI.Properties.Resources.attributes_display_icon.GetThumbnailImage(16, 16, null, IntPtr.Zero);
            attributesColumn.Width = 200;
            
            DataGridViewImageColumn rulesColum = new DataGridViewImageColumn();
            rulesColum.HeaderText = "Reglas";
            rulesColum.Image = global::BizzUI.Properties.Resources.ruler_icon;


            DataGridViewImageColumn outboxesColum = new DataGridViewImageColumn();
            outboxesColum.HeaderText = "Bandejas de salida";
            //Image outbox = global::BizzUI.Properties.Resources.outboxIcon;

            outboxesColum.Image = global::BizzUI.Properties.Resources.outboxIcon.GetThumbnailImage(16, 16, null, IntPtr.Zero);

            dgMessages.Columns.Add(attributesColumn);
            dgMessages.Columns.Add(rulesColum);
            dgMessages.Columns.Add(outboxesColum);
          
            foreach (DataGridViewColumn c in dgMessages.Columns)
            {
                c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }
            dgMessages.Columns["Title"].HeaderText = "Título";
            dgMessages.Columns["Inbox"].HeaderText = "Bandeja de entrada";
            dgMessages.Columns["MsgDate"].HeaderText = "Recibido";
            
            dgMessages.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgMessages.RowHeadersVisible = false;
            dgMessages.Columns[0].Visible = false;


        }

        private void dgMessages_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                int idMsg = (Int32)dgMessages.CurrentRow.Cells["Id"].Value;
                switch (e.ColumnIndex)
                {
                    case 0:
                     {

                         List<MessageAttributeEntity> attributes = BizzSystem.getInstance().getMesageAttributes(idMsg);
                         new MessageAttributesForm(dgMessages.CurrentRow.Cells["Title"].Value.ToString(), attributes, new string[] {"Atributo", "Tipo", "Valor" }).ShowDialog();
                         break;
                     }
                    case 1:
                     {
                         List<RuleMatchedEntity> rules = BizzSystem.getInstance().getMessageRules(idMsg);
                         new MessageAttributesForm(dgMessages.CurrentRow.Cells["Title"].Value.ToString(), rules, new string[] { "Regla" }).ShowDialog();
                         break;
                     }
                    case 2:
                     {
                         List<OutboxEntity> outboxes = BizzSystem.getInstance().getMessageOutBoxes(idMsg);
                         new MessageAttributesForm(dgMessages.CurrentRow.Cells["Title"].Value.ToString(), outboxes, new string[] {"Bandeja de salida" }).ShowDialog();
                         break;
                     }

                }
                
            }
            
        }

        private void tsFilterButton_Click(object sender, EventArgs e)
        {
            FilterForm filter = new FilterForm();
            filter.ShowDialog();
            //initMessageHistory(filter.getFilterResult());
            dgMessages.DataSource = filter.getFilterResult();

        }

       
    }
}
